package com.muqi.app.pj.shops;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.flyco.banner.anim.select.RotateEnter;
import com.flyco.banner.anim.unselect.NoAnimExist;
import com.flyco.widget.BannerItem;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.pj.shops.ShopDetialBean.Tupian;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

import com.muqi.app.user.widget.SimpleImageBanner;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class ShopDetialActivity extends BaseFragmentActivity {
	private SimpleImageBanner mBanner;
	private TextView mTv_free;
	private TextView mTv_remian;
	private Button mBt_exchange;
	private WebView mWv_detail;
	private String shop_id;
	private ShopDetialBean detialBean;
	private boolean isXuni;
	private String credits;
	
	private FictitiousShopInfo info;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shop_detial);
	}

	@Override
	protected void onInit() {
		mBanner = (SimpleImageBanner) findViewById(R.id.home_simple_banner);
		mTv_free = (TextView) findViewById(R.id.shop_free);
		mTv_remian = (TextView) findViewById(R.id.shop_remain);
		mBt_exchange = (Button) findViewById(R.id.shop_exchange);
		mWv_detail = (WebView) findViewById(R.id.shop_detail);
		// 支持javascript
		mWv_detail.getSettings().setJavaScriptEnabled(true);

		mWv_detail.getSettings().setUseWideViewPort(true);
		// 自适应屏幕
		mWv_detail.getSettings().setLayoutAlgorithm(
				LayoutAlgorithm.SINGLE_COLUMN);
		mWv_detail.getSettings().setLoadWithOverviewMode(true);

		mBt_exchange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(isXuni){
					Intent intent=new Intent(mContext, BuyXuniGoodActivity.class);
					intent.putExtra(BuyXuniGoodActivity.GOODS_DETAILS_BEAN, info);
					startActivity(intent);
				}else{
					if (detialBean != null) {
						int kucun = Integer.parseInt(detialBean.kucun);
						if (kucun > 0) {
							Intent intent = new Intent(ShopDetialActivity.this,
									BuyNowActivity.class);
							intent.putExtra(BuyNowActivity.GOODS_DETAILS_BEAN,
									detialBean);
							startActivity(intent);
						} else {
							showToast("已经被兑换完了,换个其他商品兑换吧");
						}
					}
				}
				
			}
		});

	}

	@Override
	protected void onData() {
		Intent intent = getIntent();
		if (intent != null) {
			shop_id = intent.getStringExtra("shop_id");
			isXuni = intent.getBooleanExtra("xuni", false);
			
		}
		if (!isXuni) {
			getShopDetial();
		} else {
			getXuniDetial();
			
		}

		getShopWebDetial();

	}

	private void getXuniDetial() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("id", shop_id);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.SHOP_XUNI_GOODS_DETIAL, sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				info=ResponseUtils.getXuniGoodsDetial(mContext, result);
						
				if (info != null) {
					mTv_free.setText(info.credits);
					mTv_remian.setText(info.num+"");
				}
			}
		});
		
	}

	/**
	 * 获得商品网页的详细介绍
	 * 
	 */
	private void getShopWebDetial() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		String requestUrl = null;
		if (isXuni) {
			sendMap.put("id", shop_id);
			requestUrl = ParamsUtils.buildParams(
					NetWorkApi.SHOP_XUNI_DETAIL_API, sendMap);
		} else {
			sendMap.put("shangpin_id", shop_id);
			requestUrl = ParamsUtils.buildParams(
					NetWorkApi.SHOP_GOODS_DETIAL_WEB, sendMap);
		}

		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {

					JSONObject obj;
					try {
						obj = new JSONObject(result);
						if (obj != null) {
							String url = obj.getString("data");
							mWv_detail.loadUrl(url);
						}

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});

	}

	/**
	 * 获得商品的详细信息
	 */
	private void getShopDetial() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("shangpin_id", shop_id);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.SHOP_GOODS_DETIAL, sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				ShopDetialBean bean = ResponseUtils.getShopGoodsDetial(
						mContext, result);
				if (bean != null) {
					detialBean = bean;
					Log.e("===", detialBean.tupian.size() + "  ");
					display(bean);
				}
			}
		});
	}

	protected void display(ShopDetialBean bean) {
		List<BannerItem> adListInfo = new ArrayList<BannerItem>();
		ArrayList<Tupian> bannerDatas = bean.tupian;
		if (bannerDatas != null && bannerDatas.size() > 0) {
			for (Tupian item : bannerDatas) {
				BannerItem info = null;
				info = new BannerItem();
				info.imgUrl = item.fileurl.fileurl_02;
				info.title = bean.mingcheng;
				adListInfo.add(info);
			}
			mBanner.setSelectAnimClass(RotateEnter.class)
					.setUnselectAnimClass(NoAnimExist.class).setDelay(3)
					.setPeriod(3).setSource(adListInfo).startScroll();
			mBanner.setOnItemClickL(new SimpleImageBanner.OnItemClickL() {

				@Override
				public void onItemClick(int position) {

				}
			});
		}

		mTv_free.setText(bean.danjia);
		mTv_remian.setText(bean.kucun);
	}

	public void onBack(View v) {
		finish();
	}
	

}
